package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.*;
import com.shycloud.mido.member.api.feign.MasterFeignService;
import com.shycloud.mido.member.api.vo.SmMemberTeacherGroupVO;
import com.shycloud.mido.member.api.vo.SmMemberTeacherRemarkVO;
import com.shycloud.mido.member.api.vo.StudentVO;
import com.shycloud.mido.member.api.vo.TeacherVO;
import com.shycloud.mido.member.mapper.MultiStudentClassroomLinkMapper;
import com.shycloud.mido.member.mapper.OrgRelationshipUnlinkLogMapper;
import com.shycloud.mido.member.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.shycloud.mido.common.core.util.R;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 师生管理 controller
 *
 * @author nianhua.jiang
 * @description
 * @date 2020/6/19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/teacherStudent")
@ApiIgnore
public class TeacherStudentController {

	/** 用户模块 service */
	private MemberServiceV2 memberService;
	/** 教师模块 service */
	private TeacherService teacherService;
	/** 学生模块 service */
	private OrgStudentDetailedService studentService;
	/** 教师分组模块 service */
	private SmMemberTeacherGroupService smMemberTeacherGroupService;
	/** 教师分组内学生模块 service */
	private SmMemberGroupStudentService memberGroupStudentService;
	/** 学生备注 service */
	private SmMemberTeacherRemarkService remarkService;
	/** 教师学生解绑记录表 */
	private OrgRelationshipUnlinkLogMapper unlinkLogMapper;
	/** 纠错大师 feign 模块 service */
	private MasterFeignService masterFeignService;

	private MultiStudentClassroomLinkMapper studentClassroomLinkMapper;

	/** paramter 参数：orgId */
	private static final String PARAM_ORG_ID = "orgId";
	/** paramter 参数：studentIdArr */
	private static final String PARAM_STUDENT_ID_ARR = "studentIdArr";
	/** paramter 参数：name */
	private static final String PARAM_GROUP_NAME = "name";
	/** paramter 参数：id */
	private static final String PARAM_GROUP_ID = "id";
	/** paramter 参数：groupId */
	private static final String PARAM_GROUP_ID_V = "groupId";

	/** 返回信息：批量移除学生成功 */
	private static final String MSG_REMOVE_STUDENT_SUCCESS = "批量移除学生成功";

	/**
	 * 获取学生列表
	 *
	 * @param current
	 * @param size
	 * @param orgId
	 * @param groupId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/19 18:13
	 */
	@GetMapping("getStudentDetail")
	@ApiOperation("获取学生列表")
	public R getStudentDetail(Integer current, Integer size, Integer orgId, Integer groupId, String version) {

		//TODO 分页参数
		Page page = new Page(current, 10000);
		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();

		IPage<StudentVO> studentDetailIPage = teacherService.getStudentDetail(page, orgId, groupId, memberId);

		List<StudentVO> students = studentDetailIPage.getRecords();

		for (StudentVO student : students) {
			student.setIsMaster(masterFeignService.checkMemberMasterFast(student.getMemberId()));
		}
		studentDetailIPage.setRecords(students);

		return new R(studentDetailIPage);

	}

	/**
	 * 获取学生列表
	 *
	 * @param current
	 * @param size
	 * @param orgId
	 * @param groupId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/19 18:13
	 */
	@GetMapping("getStudentDetailV2")
	@ApiOperation("获取学生列表")
	public R getStudentDetailV2(Integer current, Integer size, Integer orgId, Integer groupId, String version) {

		//TODO 分页参数
		Page page = new Page(current, 10000);
		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();

		IPage<StudentVO> studentDetailIPage = teacherService.getStudentDetail(page, orgId, groupId, memberId);

		List<StudentVO> students = studentDetailIPage.getRecords();

		//获取教师下的学生信息
		List<StudentVO> studentClass = studentClassroomLinkMapper.getStudents(memberId, students);
		if (!CollectionUtils.isEmpty(studentClass)) {
			students.addAll(studentClass);
		}

		for (StudentVO student : students) {
			student.setIsMaster(masterFeignService.checkMemberMasterFast(student.getMemberId()));
		}
		studentDetailIPage.setRecords(students);

		return new R(studentDetailIPage);

	}

	/**
	 * 获取此教师下的学生个数
	 *
	 * @param orgId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/23 17:53
	 */
	@GetMapping("getStudentCount")
	@ApiOperation("获取此教师下的学生个数")
	public R getStudentCount(Integer orgId) {

		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();

		return new R(teacherService.getStudentCount(orgId, memberId));

	}

	/**
	 * 教师获取学生分组列表
	 *
	 * @param orgId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/22 11:16
	 */
	@GetMapping("getGroupList")
	@ApiOperation("教师获取学生分组列表")
	public R getGroupList(Integer orgId) {

		//全部
		//IPage<StudentVO> studentDetail = teacherService.getStudentDetail(new Page(1, 1), orgId, 0, SecurityUtils.getMember().getId());
		//未分组学生数
		IPage<StudentVO> studentDetail2 = teacherService
				.getStudentDetail(new Page(1, 1), orgId, -1, SecurityUtils.getMember().getId());

		List<SmMemberTeacherGroupVO> groupList = new ArrayList<>();

		//全部学生Tag
		//SmMemberTeacherGroupVO allStudent = new SmMemberTeacherGroupVO();
		//allStudent.setId(0);
		//allStudent.setName("我的学生");
		//allStudent.setStudentCount(studentDetail.getTotal());
		//groupList.add(allStudent);
		//未分组学生
		SmMemberTeacherGroupVO nonStudent = new SmMemberTeacherGroupVO();
		nonStudent.setId(-1);
		nonStudent.setName("未分组");
		nonStudent.setStudentCount((int) studentDetail2.getTotal());
		groupList.add(nonStudent);
		//其余分组学生
		groupList.addAll(teacherService.getGroupList(orgId));

		return new R(groupList);
	}

	/**
	 * 教师 新建/编辑 学生分组
	 *
	 * @param requestMap
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/22 11:32
	 */
	@PostMapping("saveGroup")
	@ApiOperation("教师 新建/编辑 学生分组")
	public R saveGroup(@RequestBody Map<String, Object> requestMap) {

		//分组名称
		String name = (String) requestMap.get(PARAM_GROUP_NAME);
		//机构ID
		Integer orgId = (Integer) requestMap.get(PARAM_ORG_ID);
		//id
		Integer id = (Integer) requestMap.get(PARAM_GROUP_ID);

		//返回结果
		return smMemberTeacherGroupService.saveGroup(name, orgId, id);

	}

	/**
	 * 教师删除学生分组
	 *
	 * @param groupIdArr
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/30 9:57
	 */
	@PostMapping("removeGroup")
	@ApiOperation("教师删除学生分组")
	public R removeGroup(@RequestBody List<Integer> groupIdArr) {
		return new R(smMemberTeacherGroupService.removeGroup(groupIdArr));
	}


	/**
	 * 批量添加学生进分组
	 *
	 * @param requestMap
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/22 13:38
	 */
	@PostMapping("addStudentList")
	@ApiOperation("批量添加学生进分组")
	public R addStudentList(@RequestBody Map<String, Object> requestMap) {

		//添加的学生ID数组
		List<String> studentIdArr = (List<String>) requestMap.get(PARAM_STUDENT_ID_ARR);
		//机构Id
		Integer groupId = (Integer) requestMap.get(PARAM_GROUP_ID_V);

		return new R(memberGroupStudentService.addStudentList(studentIdArr, groupId));
	}

	/**
	 * 从分组中批量移除学生
	 *
	 * @param requestMap
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/22 13:38
	 */
	@PostMapping("removeStudentList")
	@ApiOperation("从分组中批量移除学生")
	public R removeStudentList(@RequestBody Map<String, Object> requestMap) {

		//添加的学生ID数组
		List<String> studentIdArr = (List<String>) requestMap.get(PARAM_STUDENT_ID_ARR);
		//机构Id
		Integer groupId = (Integer) requestMap.get(PARAM_GROUP_ID_V);

		return new R(memberGroupStudentService.removeStudentList(studentIdArr, groupId));
	}

	@PostMapping("remark")
	@ApiOperation("教师为学生设置备注")
	public R remark(@RequestBody SmMemberTeacherRemark remark) {
		return new R(remarkService.remark(remark));
	}

	/**
	 * 三期在线可教师为学生设置备注及分组
	 *
	 * @param remark
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/10 10:06
	 **/
	@PostMapping("remarkForOnlineCourse")
	@ApiOperation("三期在线可教师为学生设置备注及分组")
	public R remarkForOnlineCourse(@RequestBody SmMemberTeacherRemarkVO remark) {

		//更新学生备注等基本信息
		remarkService.remark(remark);
		//转移分组并返回结果
		List<String> studentIdArr = new ArrayList<>();
		studentIdArr.add(remark.getStudentId());
		return new R(memberGroupStudentService.addStudentList(studentIdArr, remark.getGroupId()));
	}

	/**
	 * 获取我的老师
	 *
	 * @param orgId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/22 17:41
	 */
	@GetMapping("getMyTeacher")
	@ApiOperation("获取我的老师")
	public R getMyTeacher(Integer orgId) {
		return new R(teacherService.getMyTeacher(SecurityUtils.getMember().getId(), orgId));
	}

	/**
	 * 获取教师ID（feign用）
	 *
	 * @param
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/6/24 11:22
	 */
	@GetMapping("getTeacherId")
	public String getTeacherId(Integer orgId) {
		return teacherService.getTeacher(orgId, SecurityUtils.getMember().getId()).getId();
	}

	/**
	 * 根据学生ID获取教师ID（feign用）
	 *
	 * @param
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/6/24 11:22
	 */
	@GetMapping("getCurTeacIdByStu")
	public String getCurTeacherIdByStudentId(String studentId) {
		return teacherService.getCurTeacherIdByStudentId(studentId);
	}

	/**
	 * 根据学生ID获取教师ID（feign用）
	 *
	 * @param
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/6/24 11:22
	 */
	@GetMapping("getTeacIdsByStu")
	public List<String> getTeacherIdsByStudentId(String studentId) {
		return teacherService.getTeacherIdsByStudentId(studentId);
	}

	/**
	 * 获取学生ID（feign用）
	 *
	 * @param
	 * @return java.lang.String
	 * @author nianhua.jiang
	 * @date 2020/6/24 11:22
	 */
	@GetMapping("getStudentId")
	public String getStudentId(String memberId) {

		OrgStudentDetailed orgStudentDetailed = studentService.getOne(new QueryWrapper<OrgStudentDetailed>()
				.eq(OrgStudentDetailed.COL_MEMBER_ID, memberId)
				.and(y -> y.eq(OrgStudentDetailed.COL_DEL_FLAG, CommonConstants.STATUS_NORMAL)));

		if (orgStudentDetailed == null) {
			return "0";
		}
		return orgStudentDetailed.getId();
	}

	/**
	 * 扫码添加学生
	 *
	 * @param map
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/30 19:48
	 */
	@PostMapping("addStudent")
	@ApiOperation("扫码添加学生")
	public R addStudent(@RequestBody Map<String, Object> map) {
		return new R(memberService.addStudent(map));
	}

	@PostMapping("addStudentByUsername")
	@ApiOperation("通过账号添加学生")
	public R addStudentByUsername(@RequestBody Map<String, Object> resMap) {
		return new R(memberService.addStudentByUsername(resMap));
	}

	/**
	 * 批量移除学生
	 *
	 * @param map
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/8/3 10:49
	 */
	@PostMapping("removeStudentBatch")
	@ApiOperation("批量移除学生")
	public R removeStudentBatch(@RequestBody Map<String, Object> map) {
		// 要移除学生的ID集合
		List<String> studentIdArr = (List<String>) map.get(PARAM_STUDENT_ID_ARR);
		// 机构ID
		Integer orgId = (Integer) map.get(PARAM_ORG_ID);

		// 返回操作结果
		return new R(CommonConstants.SUCCESS, MSG_REMOVE_STUDENT_SUCCESS, teacherService.removeStudentBatch(studentIdArr, orgId));
	}

	/**
	 * 家长模式获取学生列表
	 *
	 * @param orgId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/9/5 8:58
	 **/
	@GetMapping("getStudentByParent")
	public R getStudentByParent(Integer orgId) {

		//获取用户ID
		String memberId = SecurityUtils.getMember().getId();

		return new R(teacherService.getStudentByParent(orgId, memberId));
	}

	/**
	 * 通过学生ID获取学生信息
	 *
	 * @param studentId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/5 10:36
	 **/
	@GetMapping("getStudentInfo")
	@ApiOperation("获取学生详情")
	public R getStudentDetail(String studentId) {
		return new R(teacherService.getStudentInfo(studentId));
	}

	/**
	 * 通过学生ID获取学生信息 - feign
	 *
	 * @param studentId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/5 10:36
	 **/
	@GetMapping("getStudentInfoFeign")
	@ApiOperation("获取学生详情")
	public StudentVO getStudentInfoFeign(String studentId) {
		return teacherService.getStudentInfo(studentId);
	}

	/**
	 * 通过教师ID获取教师信息 - feign
	 *
	 * @param teacherId
	 * @return
	 * @author nianhua.jiang
	 * @date 2021/8/17 9:03
	 **/
	@GetMapping("getTeacherInfoFeign")
	@ApiOperation("获取教师详情")
	public TeacherVO getTeacherInfoFeign(String teacherId) {
		return teacherService.getTeacherInfo(teacherId);
	}


	/**
	 * 通过学生ID获取学生当前和下个阶段作业布置剩余可用数量
	 *
	 * @param studentId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/5 10:36
	 **/
	@GetMapping("getStuAssignBal")
	@ApiOperation("获取学生作业布置剩余可用数量")
	public Map getStudentAssignBal(String studentId) {
		return teacherService.getStudentAssignBal(studentId);
	}

	/**
	 * 消耗学生作业布置剩余可用数量
	 *
	 * @param studentId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/5 10:36
	 **/
	@GetMapping("consumeAssignBal")
	@ApiOperation("消耗学生作业布置剩余可用数量")
	public Boolean consumeAssginBal(String studentId, Integer curConsume, Integer nextConsume) {
		return teacherService.consumeAssginBal(studentId, curConsume, nextConsume);
	}

	/**
	 * 搜索学生
	 *
	 * @param name
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/5 14:24
	 **/
	@GetMapping("searchStudent")
	public R searchStudent(Integer current, Integer size, String name, String teacherId) {
		return new R(teacherService.searchStudent(current, size, name, teacherId));
	}

	/**
	 * 解除师生关系
	 *
	 * @param studentId
	 * @param teacherId
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/11/5 17:37
	 **/
	@GetMapping("dismiss")
	public R dismiss(String studentId, String teacherId, String identity) {
		return new R(teacherService.dismiss(studentId, teacherId, identity));
	}

	@GetMapping("getStudentList")
	public R getStudentList(String teacherId) {
		return new R(teacherService.getStudentList(teacherId));
	}

	@GetMapping("getStudent")
	public R getStudent(String studentId) {
		return new R(teacherService.getStudent(studentId));
	}


	/**
	 * 教师学生解绑记录查询(暂供feign调用)
	 *
	 * @param studentId
	 * @return com.shycloud.mido.common.core.util.R
	 * @author xiaoxing.yu
	 * @date 2020/12/4 上午10:31
	 */
	@GetMapping("getUnlinkLog")
	public List<OrgRelationshipUnlinkLog> getUnlinkLog(String studentId) {
		return unlinkLogMapper.selectUniqueTeacherLogs(studentId);
	}


	@PostMapping("updGroupSort")
	public R updGroupSort(@RequestBody Map<String, List<Integer>> resMap) {

		List<Integer> ids = resMap.get("ids");

		for (int i = 0; i < ids.size(); i++) {

			if (ids.get(i) != -1) {
				SmMemberTeacherGroup group = smMemberTeacherGroupService.getById(ids.get(i));

				group.setSort(i);

				smMemberTeacherGroupService.updateById(group);
			}
		}

		return new R(true);
	}

}
